package com.getpebble.android.framework.appmessage;

import com.getpebble.android.framework.appmessage.PebbleTuple.TupleType;
import com.getpebble.android.framework.appmessage.PebbleTuple.Width;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.json.JSONObject;

public abstract class PebbleDictionary implements Iterable<PebbleTuple> {
    protected final Map<Integer, PebbleTuple> tuples = new HashMap();

    public static class PebbleDictTypeException extends RuntimeException {
        public PebbleDictTypeException(long key, TupleType expected, TupleType actual) {
            super(String.format("Expected type '%s', but got '%s' for key 0x%08x", new Object[]{expected.name(), actual.name(), Long.valueOf(key)}));
        }
    }

    public static class TupleOverflowException extends RuntimeException {
        public TupleOverflowException() {
            super("Too many tuples in dict");
        }
    }

    public PebbleDictionary(PebbleDictionary other) {
        if (other != null) {
            this.tuples.putAll(other.tuples);
        }
    }

    public synchronized Iterator<PebbleTuple> iterator() {
        return this.tuples.values().iterator();
    }

    public void addBytes(int key, byte[] bytes) {
        addTuple(PebbleTuple.create(key, TupleType.BYTES, Width.NONE, (Object) bytes));
    }

    public void addBytesCopy(int key, byte[] bytes) {
        Object bytes2;
        TupleType tupleType = TupleType.BYTES;
        Width width = Width.NONE;
        if (bytes != null) {
            bytes2 = bytes.length > 0 ? Arrays.copyOf(bytes, bytes.length) : new byte[0];
        }
        addTuple(PebbleTuple.create(key, tupleType, width, bytes2));
    }

    public void addString(int key, String value) {
        addTuple(PebbleTuple.create(key, TupleType.STRING, Width.NONE, (Object) value));
    }

    public void addInt8(int key, byte b) {
        addTuple(PebbleTuple.create(key, TupleType.INT, Width.BYTE, (int) b));
    }

    public void addUint8(int key, byte b) {
        addTuple(PebbleTuple.create(key, TupleType.UINT, Width.BYTE, (int) b));
    }

    public void addInt16(int key, short s) {
        addTuple(PebbleTuple.create(key, TupleType.INT, Width.SHORT, (int) s));
    }

    public void addUint16(int key, short s) {
        addTuple(PebbleTuple.create(key, TupleType.UINT, Width.SHORT, (int) s));
    }

    public void addInt32(int key, int i) {
        addTuple(PebbleTuple.create(key, TupleType.INT, Width.WORD, i));
    }

    public void addUint32(int key, int i) {
        addTuple(PebbleTuple.create(key, TupleType.UINT, Width.WORD, i));
    }

    private synchronized PebbleTuple getTuple(int key, TupleType type) {
        PebbleTuple pebbleTuple;
        if (!this.tuples.containsKey(Integer.valueOf(key)) || this.tuples.get(Integer.valueOf(key)) == null) {
            pebbleTuple = null;
        } else {
            pebbleTuple = (PebbleTuple) this.tuples.get(Integer.valueOf(key));
            if (pebbleTuple.type != type) {
                throw new PebbleDictTypeException((long) key, type, pebbleTuple.type);
            }
        }
        return pebbleTuple;
    }

    public Long getUnsignedInteger(int key) {
        PebbleTuple tuple = getTuple(key, TupleType.UINT);
        if (tuple == null) {
            return null;
        }
        return (Long) tuple.value;
    }

    protected synchronized void addTuple(PebbleTuple tuple) {
        if (this.tuples.size() > 255) {
            throw new TupleOverflowException();
        }
        this.tuples.put(Integer.valueOf(tuple.key), tuple);
    }

    public synchronized JSONObject toJson() {
        JSONObject jsonObject;
        try {
            jsonObject = new JSONObject();
            JSONObject tuplesJsonObject = new JSONObject();
            for (Entry<Integer, PebbleTuple> entry : this.tuples.entrySet()) {
                PebbleTuple tuple = (PebbleTuple) entry.getValue();
                if (tuple == null) {
                    tuplesJsonObject.put("" + entry.getKey(), "((null))");
                } else {
                    tuplesJsonObject.put("" + entry.getKey(), tuple.toJson());
                }
            }
            jsonObject.put("tuples", tuplesJsonObject);
        } catch (Exception e) {
            jsonObject = null;
        }
        return jsonObject;
    }

    public String toJsonString() {
        JSONObject jsonObject = toJson();
        return jsonObject == null ? "{}" : jsonObject.toString();
    }

    public String toString() {
        return toJsonString();
    }
}
